<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkedList">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="end">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
// 根据id获取用户详情
import { userDetialInfo } from '@/api/user'
import { assignRole } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 所有的角色列表
      checkedList: [],
      // 被选中的角色列表
      roleList: []
    }
  },
  async created() {
    // 显示被选中的角色
    const { rows } = await getRoleList()
    this.roleList = rows
  },
  methods: {
    // 获取所有的角色列表
    async userDetialInfo(id) {
      const { roleIds } = await userDetialInfo(id)
      this.checkedList = roleIds
    },
    // 确定
    async btnOK() {
      // 给员工分配角色
      await assignRole({
        id: this.userId,
        roleIds: this.checkedList
      })
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    btnCancel() {
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
